/**
 * Copyright 2009 Adam Ruggles.
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.google.code.sapien.service;

import com.google.code.sapien.model.Category;
import com.google.code.sapien.security.CategoryPermissions;
import com.google.code.sapien.security.SecurityDetails;
import com.google.code.sapien.util.ContextManager;

/**
 * Default implementation of the {@link com.google.code.sapien.service.SecurityService}.
 * @author Adam
 * @version $Id$
 * 
 * Created on May 24, 2009 at 9:12:18 PM 
 */
public class SapienSecurityService implements SecurityService {

	/**
	 * {@inheritDoc}
	 * @see com.google.code.sapien.service.SecurityService#checkCategoryPermissions(
	 * com.google.code.sapien.model.Category, com.google.code.sapien.security.CategoryPermissions)
	 */
	public boolean checkCategoryPermissions(final Category category, final CategoryPermissions permissions) {
		final SecurityDetails securityDetails = ContextManager.getBind(SecurityDetails.class);
		if (securityDetails == null) {
			return false;
		}
		return securityDetails.checkCategory(category, permissions);
	}

	/**
	 * {@inheritDoc}
	 * @see com.google.code.sapien.service.SecurityService#checkCategoryPermissions(
	 * com.google.code.sapien.model.Category, int[])
	 */
	public boolean checkCategoryPermissions(final Category category, final int[] permissions) {
		final SecurityDetails securityDetails = ContextManager.getBind(SecurityDetails.class);
		if (securityDetails == null) {
			return false;
		}
		return securityDetails.checkCategory(category, new CategoryPermissions(permissions));
	}

	/**
	 * {@inheritDoc}
	 * @see com.google.code.sapien.service.SecurityService#isAdministrator()
	 */
	public boolean isAdministrator() {
		final SecurityDetails securityDetails = ContextManager.getBind(SecurityDetails.class);
		return securityDetails.isAdministrator();
	}

}
